import 'package:ding_rtc_example/tool.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import 'meeting.dart';

class LoginPage extends StatefulWidget {

  const LoginPage({super.key});

  @override
  State<StatefulWidget> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final _conferenceNumberController = TextEditingController(text: '9527');
  final _userIdController = TextEditingController(text: '1112');
  final _userNameController = TextEditingController(text: 'demo');

  @override
  void initState() {
    super.initState();
  }

  _enterMeeting() async {
    String _meetId = _conferenceNumberController.text;
    String _userId = _userIdController.text;
    String _userName = _userNameController.text;
    if (_meetId == '') {
      MeetingTool.toast('Please enter the conference number', context);
      return;
    } else if (_meetId == '0') {
      MeetingTool.toast('Please enter the legal meeting ID', context);
      return;
    } else if (_meetId.toString().length > 10) {
      MeetingTool.toast('Please enter a valid conference ID', context);
      return;
    } else if (!RegExp(r"[0-9]+$").hasMatch(_meetId)) {
      MeetingTool.toast(
          'Conference ID can only be numeric. Please enter a legal conference ID',
          context);
      return;
    }
    if (_userId == '') {
      MeetingTool.toast('Please enter user ID', context);
      return;
    } else if (!RegExp(r"[A-Za-z0-9_]+$").hasMatch(_userId)) {
      MeetingTool.toast(
          'User ID can only be numbers, letters and underscores. Please enter the correct user ID',
          context);
      return;
    } else if (_userId.length > 10) {
      MeetingTool.toast(
          'The user ID is too long. Please enter a legal user ID', context);
      return;
    }
    if (_userName == '') {
      MeetingTool.toast('Please enter user Name', context);
      return;
    }
    _unFocus();
    if (!(await Permission.camera.request().isGranted) ||
        !(await Permission.microphone.request().isGranted)) {
      MeetingTool.toast(
          'You need to obtain audio and video permission to enter', context);
      return;
    }
    await Permission.storage.request();
    await Permission.systemAlertWindow.request();

    Navigator.of(context).push(MaterialPageRoute(builder: (con) {
      return MeetingPage(meetingId: _meetId, userId: _userId, userName: _userName);
    }));
  }

  _unFocus() {
    FocusScope.of(context).unfocus();
  }

  @override
  dispose() {
    super.dispose();
    _unFocus();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: const Text('Ding Rtc Video Demo'),
            ),
            body: Container(
                color: const Color.fromRGBO(14, 25, 44, 1),
                padding: const EdgeInsets.symmetric(horizontal: 20.0),
                child: Column(children: <Widget>[
                  TextField(
                    controller: _conferenceNumberController,
                    autofocus: false,
                    style: const TextStyle(color: Colors.white),
                    decoration: const InputDecoration(
                      labelText: "Conference number",
                      hintText: "Please enter the conference number",
                      labelStyle: TextStyle(color: Colors.white),
                      hintStyle:
                      TextStyle(color: Color.fromRGBO(255, 255, 255, 0.5)),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.white),
                      ),
                    ),
                    keyboardType: TextInputType.number,
                  ),
                  TextField(
                      style: const TextStyle(color: Colors.white),
                      autofocus: false,
                      controller: _userIdController,
                      decoration: const InputDecoration(
                        labelText: "User ID",
                        hintText: "Please enter user ID",
                        labelStyle: TextStyle(color: Colors.white),
                        hintStyle: TextStyle(
                            color: Color.fromRGBO(255, 255, 255, 0.5)),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                      ),
                      keyboardType: TextInputType.text),
                  TextField(
                      style: const TextStyle(color: Colors.white),
                      autofocus: false,
                      controller: _userNameController,
                      decoration: const InputDecoration(
                        labelText: "User Name",
                        hintText: "Please enter user Name",
                        labelStyle: TextStyle(color: Colors.white),
                        hintStyle: TextStyle(
                            color: Color.fromRGBO(255, 255, 255, 0.5)),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                      ),
                      keyboardType: TextInputType.text),
                  Container(
                      width: double.infinity,
                      margin: const EdgeInsets.only(top: 30.0),
                      child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            padding: const EdgeInsets.all(15.0),
                            backgroundColor: Theme.of(context).primaryColor,
                          ),
                          onPressed: _enterMeeting,
                          child: const Text(
                            "Enter the meeting",
                            style: TextStyle(color: Colors.white),
                          )))
                ]))));
  }
}